import { defineStore } from 'pinia'
import { accountLogin } from '@/service/login/login'
import type { IAccount } from '@/views/login/tpyes'
import { localCache } from '@/utils/cache'

const LOGIN_TOKEN = 'token'

const useLoginStore = defineStore('login', {
  state: () => {
    return {
      id: '',
      name: '',
      token: localCache.getCache(LOGIN_TOKEN) ?? ''
    }
  },
  actions: {
    async loginAccountAction(account: IAccount) {
      //账号登录 获取token等信息
      const loginResult = await accountLogin(account)
      this.id = loginResult.data.id
      this.name = loginResult.data.name
      this.token = loginResult.data.token
      //进行本地缓存
      localCache.setCache(LOGIN_TOKEN, this.token)
    }
  }
})

export default useLoginStore
